package com.ytzl.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JwtUtils {
    //签名
    private  static final String SIGN="213dsaf";
    //过期时间
    private static final Integer EXPIRE_TIME=7;

    /**
     * 创建token
     *
     * @param map
     * @return
     */
    public static String createToken(Map<String,String> map){
        Calendar instance=Calendar.getInstance();
        instance.add(Calendar.DATE,EXPIRE_TIME);

        JWTCreator.Builder builder= JWT.create();
        
        //payload
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        
        return builder.withExpiresAt(instance.getTime()).sign(Algorithm.HMAC256(SIGN));
    }

    public static DecodedJWT verifyToken(String token){
        return JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);

    }
}
